#include<iostream>
#include<vector>
#include<sstream>
#include<algorithm>
using namespace std;

class Book{
public:
    string id;
    string title;
    string author;
    vector<string> keywords;
    string publisher;
    string year;
    Book(string id, string title, string author, string publisher, string year){
        this->id=id;
        this->title=title;
        this->author=author;
        this->publisher=publisher;
        this->year=year;
    }
};

int N;
int M;
vector<Book> books;
vector<Book*> titles,authors,years;
vector<pair<string, vector<string> > > keywords_hashmap, publisher_hashmap;

bool titlesort(const Book* a, const Book* b){
    return a->title<b->title;
}
bool authorsort(const Book* a, const Book* b){
    return a->author<b->author;
}
bool yearsort(const Book* a, const Book* b){
    return a->year<b->year;
}

int binarysearch(int left, int right, string target, int query_type){
    int mid;
    while(left<=right){
        mid=(left+right)/2;
        switch(query_type){
        case 1:
            if(target==titles[mid]->title)
                return mid;
            else if(target<titles[mid]->title)
                right=mid-1;
            else if(target>titles[mid]->title)
                left=mid+1;
            break;
        case 2:
            if(target==authors[mid]->author)
                return mid;
            else if(target<authors[mid]->author)
                right=mid-1;
            else if(target>authors[mid]->author)
                left=mid+1;
            break;
        case 5:
            if(target==years[mid]->year)
                return mid;
            else if(target<years[mid]->year)
                right=mid-1;
            else if(target>years[mid]->year)
                left=mid+1;
            break;
        }
    }
    return -1;
}

int main(){
    ios::sync_with_stdio(false);
    cin>>N;
    cin.ignore();
    int i=0;
    while(i++<N){
        string id,title,author,publisher,keywords,year;
        getline(cin, id);
        getline(cin, title);
        getline(cin, author);
        getline(cin, keywords);
        getline(cin, publisher);
        cin>>year;
        cin.ignore();

        Book* pt1 = new Book(id,title,author,publisher,year);
        books.push_back(*pt1);
        Book* pt2=pt1;
        Book* pt3=pt1;
        titles.push_back(pt1);
        authors.push_back(pt2);
        years.push_back(pt3);

        stringstream ss(keywords);
        while(!ss.eof()){
            string tmp;
            ss>>tmp;
            bool flag = false;
            for(int j=0;j<keywords_hashmap.size();j++){
                string kw = keywords_hashmap[j].first;
                if (kw == tmp){
                    keywords_hashmap[j].second.push_back(id);
                    flag = true;
                    break;
                }
            }
            if(!flag){
                vector<string> t;
                t.push_back(id);
                keywords_hashmap.push_back(pair<string, vector<string> >(tmp, t));
            }
        }
        bool flag = false;
        for(int j=0;j<publisher_hashmap.size();j++){
            string pb = publisher_hashmap[j].first;
            if (pb == publisher){
                publisher_hashmap[j].second.push_back(id);
                flag = true;
            }
        }
        if(!flag){
            vector<string> t;
            t.push_back(id);
            publisher_hashmap.push_back(pair<string, vector<string> >(publisher, t));
        }

    }

    sort(titles.begin(),titles.end(),titlesort);
    sort(authors.begin(),authors.end(),authorsort);
    sort(years.begin(),years.end(),yearsort);

    //cout<<"publisher_hashmap.size():"<<publisher_hashmap.size()<<endl;
    //for(int i=0;i<publisher_hashmap.size();i++){
    //    cout<<"publisher:"<<publisher_hashmap[i].first<<" "<<publisher_hashmap[i].second.size()<<endl;
    //}

    cin>>M;
    cin.ignore();
    i=0;
    vector<pair<int,string> > querys;
    while(i++<M){
        string query;
        getline(cin,query);
        int query_type = query[0]-'0';
        string query_content = query.substr(3);
        querys.push_back(pair<int,string>(query_type, query_content));
    }
    //cout<<"querys.size():"<<querys.size()<<endl;
    for(i=0;i<querys.size();i++){
        int query_type = querys[i].first;
        string query_content = querys[i].second;
        cout<<query_type<<": "<<query_content<<endl;
        if(query_type==1){//title
            int res = binarysearch(0,N-1,query_content,1);
            if (res<0){
                cout<<"Not Found"<<endl;
            }else{
                int m=res-1;
                int n=res+1;
                vector<string> res_ids;
                while(m>=0){
                    if(query_content==titles[m]->title){
                        res_ids.push_back(titles[m]->id);
                    }else{
                        break;
                    }
                    m--;
                }
                while(n<=N-1){
                    if(query_content==titles[n]->title){
                        res_ids.push_back(titles[n]->id);
                    }else{
                        break;
                    }
                    n++;
                }
                res_ids.push_back(titles[res]->id);
                sort(res_ids.begin(),res_ids.end());
                for(m=0;m<res_ids.size();m++)
                    cout<<res_ids[m]<<endl;
            }
        }else if(query_type==2){//author
            int res = binarysearch(0,N-1,query_content,2);
            if (res<0){
                cout<<"Not Found"<<endl;
            }else{
                int m=res-1;
                int n=res+1;
                vector<string> res_ids;
                while(m>=0){
                    if(query_content==authors[m]->author){
                        res_ids.push_back(authors[m]->id);
                    }else{
                        break;
                    }
                    m--;
                }
                while(n<=N-1){
                    if(query_content==authors[n]->author){
                        res_ids.push_back(authors[n]->id);
                    }else{
                        break;
                    }
                    n++;
                }
                res_ids.push_back(authors[res]->id);
                sort(res_ids.begin(),res_ids.end());
                for(m=0;m<res_ids.size();m++)
                    cout<<res_ids[m]<<endl;
            }
        }else if(query_type==5){//years
            int res = binarysearch(0,N-1,query_content,5);
            if (res<0){
                cout<<"Not Found"<<endl;
            }else{
                int m=res-1;
                int n=res+1;
                vector<string> res_ids;
                while(m>=0){
                    if(query_content==years[m]->year){
                        res_ids.push_back(years[m]->id);
                    }else{
                        break;
                    }
                    m--;
                }
                while(n<=N-1){
                    if(query_content==years[n]->year){
                        res_ids.push_back(years[n]->id);
                    }else{
                        break;
                    }
                    n++;
                }
                res_ids.push_back(years[res]->id);
                sort(res_ids.begin(),res_ids.end());
                for(m=0;m<res_ids.size();m++)
                    cout<<res_ids[m]<<endl;
            }
        }else if(query_type==3){//keyword
            bool flag=false;
            for(int j=0;j<keywords_hashmap.size();j++){
                if(query_content==keywords_hashmap[j].first){
                    flag=true;
                    vector<string> ids = keywords_hashmap[j].second;
                    sort(ids.begin(),ids.end());
                    for(int k=0;k<ids.size();k++){
                        cout<<ids[k]<<endl;
                    }
                    break;
                }
            }
            if(!flag)
                cout<<"Not Found"<<endl;

        }else if(query_type==4){//publisher
            bool flag=false;
            for(int j=0;j<publisher_hashmap.size();j++){
                if(query_content==publisher_hashmap[j].first){
                    flag=true;
                    vector<string> ids = publisher_hashmap[j].second;
                    sort(ids.begin(),ids.end());
                    for(int k=0;k<ids.size();k++){
                        cout<<ids[k]<<endl;
                    }
                    break;
                }
            }
            if(!flag)
                cout<<"Not Found"<<endl;
        }
    }
    return 0;
}
